package cn.ygh.bbo.system.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import cn.ygh.bbo.framerwork.core.Grid;
import cn.ygh.bbo.common.exception.TipException;
import cn.ygh.bbo.common.annotation.DataScope;
import cn.ygh.bbo.system.bean.DataFilter;
import cn.ygh.bbo.system.mapper.DataFilterMapper;
import cn.ygh.bbo.system.service.DataFilterService;
import org.springframework.transaction.annotation.Transactional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 描述：数据过滤  ServiceImpl
 * @author ygh
 */
@Service
public class DataFilterServiceImpl implements DataFilterService{

    private final Logger logger=LoggerFactory.getLogger(getClass());

    @Autowired
    DataFilterMapper mapper;

    /**
    * 保存对象
    * @param  dataFilter 对象信息
    * @return
    */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int save(DataFilter dataFilter) {
        //保存之前通用处理
        beforeCreate(dataFilter);
        dataFilter.def();
        return mapper.insert(dataFilter);
    }

    /**
    * 更新对象
    * @param  dataFilter 对象信息
    * @return
    */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int update(DataFilter dataFilter) {
        if(dataFilter.getId()==null){
            throw new TipException("id can not null");
        }
        //更新之前通用处理
        beforeUpdate(dataFilter);
        dataFilter.def();
        return mapper.updateById(dataFilter);
    }

    /**
    * 批量更新
    * @param  dataFilter 对象信息
    * @param ids
    * @return
    */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updateBatchByIds(DataFilter dataFilter, Serializable[] ids) {
        if(ids==null || ids.length==0){
            throw new TipException("ids can not null");
        }
        return mapper.updateBatchByIds(dataFilter,ids);
    }

    /**
    * 根据ID删除对象
    * @param id
    * @return
    */
    @Override
    public int deleteById(Serializable id) {
        if(id==null){
            throw new TipException("id can not null");
        }
        if(checkLink(id)){
            return mapper.deleteById(id);
        }
        return 0;
    }

    /**
    * 删除关联判断
    * @param id
    */
    private boolean checkLink(Serializable id) {
        return true;
    }

    /**
    * 根据ID数组批量删除对象
    * @param ids
    * @return
    */
    @Override
    public int deleteByIds(Serializable[] ids) {
        if(ids==null || ids.length==0){
            throw new TipException("ids can not null");
        }
        int result=0;
        for (Serializable id:ids){
            result+=deleteById(id);
        }
        return result;
    }

    /**
    * 根据ID获取对象
    * @param id
    * @return
    */
    @Override
    public DataFilter getById(Serializable id) {
        if(id==null){
            throw new TipException("id can not null");
        }
        return mapper.selectById(id);
    }

    /**
    * 根据ID数组获取对象列表
    * @param ids
    * @return
    */
    @Override
    public List<DataFilter> listfindByIn(Serializable[] ids) {
        if(ids==null || ids.length==0){
            throw new TipException("ids can not null");
        }
        return mapper.selectBatchIds(Arrays.asList(ids));
    }

    /**
    * 根据对象信息获取满足条件的对象列表
    * @param  dataFilter 对象信息
    * @param pageable 分页条件为空则不分页
    * @return
    */
    @DataScope
    @Override
    public List<DataFilter> listfind(DataFilter dataFilter, Pageable pageable) {
        if (pageable!=null && pageable.getPageNumber()!=0){
            PageHelper.startPage(pageable.getPageNumber()
                    , pageable.getPageSize(), false);
        }
        return mapper.selectByObj(dataFilter);
    }

    /**
    * 根据对象信息获取满足条件的对象列表和总数
    * @param  dataFilter 对象信息
    * @param pageable 分页条件
    * @return
    */
    @DataScope
    @Override
    public Grid listGrid(DataFilter dataFilter, Pageable pageable) {
        PageHelper.startPage(pageable.getPageNumber()
                , pageable.getPageSize(), true);
        List<DataFilter> dataFilters = mapper.selectByObj(dataFilter);
        PageInfo<DataFilter>pageInfo=new PageInfo<>(dataFilters);
        Grid grid=new Grid();
        grid.setTotal((int) pageInfo.getTotal());
        grid.setRows(pageInfo.getList());
        return grid;
    }

    /**
     * 通过roleId列表获取数据过滤权限
     *
     * @param roleIds
     * @return
     */
    @Override
    public List<DataFilter> listfindByRoleIds(List<Integer> roleIds) {
        if (roleIds.size()==0){
            return new ArrayList<>();
        }
        DataFilter params=new DataFilter();
        if (roleIds.size()==1){
            params.setRoleId(roleIds.get(0));
        }else {
            params.setRoleIds(roleIds);
        }
        return listfind(params, null);
    }

    /**
     * 角色数据权限数量
     *
     * @param roleId
     * @return
     */
    @Override
    public long getCountByRoleId(Integer roleId) {
        DataFilter params=new DataFilter();
        params.setRoleId(roleId);
        return mapper.selectCountByObj(params);
    }
}
